package com.yimi.yinhepda.views.main.my_module;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.pda.ym.callbacks.UICallBack;
import com.pda.ym.utils.SoundVibratorManager;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.RecycleAdapter.StorageInfoAdapter;
import com.yimi.yinhepda.db.dao.StorageInfoTableDao;
import com.yimi.yinhepda.db.table.StorageInfoTable;
import com.yimi.yinhepda.base.BaseInitActivity;
import com.yimi.yinhepda.entity.request.QueryStorageInfoRequest;
import com.yimi.yinhepda.entity.response.QueryStorageInfoResponse;
import com.yimi.yinhepda.views.stockCheck_module.StockCheckUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author: lj
 * Date: 2019/10/24 16:40
 * Description: 分拨货区界面
 */
public class StorageInfoActivity extends BaseInitActivity implements TextWatcher {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_storage_info)
    EditText etStorageInfo;
    @Bind(R.id.rv_storage_info)
    RecyclerView rvStorageInfo;

    /**
     * 货区集合
     */
    private List<StorageInfoTable> mStorageInfoList = new ArrayList<>();
    private List<StorageInfoTable> mTempList = new ArrayList<>();
    private StorageInfoAdapter mAdapter;

    @Override
    public String getPageName() {
        return "分拨货区界面";
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_storage_info;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }

    @Override
    public void initEvent() {
        etStorageInfo.addTextChangedListener(this);
    }

    @Override
    public void initData() {
        tvTitle.setText("分拨货区");

        QueryStorageInfoRequest request = new QueryStorageInfoRequest();
        request.setDeptCode(sharedPreferUtil.getDeptCode());
        StockCheckUtil.queryStorageInfo(request, new UICallBack() {
            @Override
            public void onError(String msg) {
                showToast(msg);
            }

            @Override
            public void onSuccess(Object obj) {
                QueryStorageInfoResponse response = (QueryStorageInfoResponse) obj;
                if (response.getData() != null) {
                    for (QueryStorageInfoRequest storageInfo : response.getData()) {
                        StorageInfoTable table = new StorageInfoTable();
                        table.setStorageCode(storageInfo.getStorageCode());
                        table.setStorageName(storageInfo.getStorageName());
                        mTempList.add(table);
                        mStorageInfoList.add(table);
                    }
                } else {
                    SoundVibratorManager.playSound(2);
                    showToast("未查询到货区信息！");
                }
                mAdapter = new StorageInfoAdapter(StorageInfoActivity.this, mTempList);
                rvStorageInfo.setLayoutManager(new LinearLayoutManager(StorageInfoActivity.this, LinearLayoutManager.VERTICAL, false));
                rvStorageInfo.setAdapter(mAdapter);
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.ll_search})
    @Override
    protected void onClickBtn(View v) {
        super.onClickBtn(v);
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.ll_search:
                mTempList.clear();
                String storageInfo = etStorageInfo.getText().toString().trim();
                if (!"".equals(storageInfo)) {
                    for (StorageInfoTable item : mStorageInfoList) {
                        if (item.getStorageCode().contains(storageInfo) || item.getStorageName().contains(storageInfo)) {
                            mTempList.add(item);
                        }
                    }
                } else {
                    mTempList.addAll(mStorageInfoList);
                }
                if (mAdapter != null) {
                    mAdapter.notifyDataSetChanged();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if ("".equals(s.toString().trim())) {
            mTempList.clear();
            mTempList.addAll(mStorageInfoList);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
}
